<?php

/**
 * 产品基本信息仓库
 * @author wangwx 2015-5-12
 */
namespace common\repositories\myhtsq;

use common\repositories\BaseRepository;
use common\support\StringHelper;
use common\entities\myhtsq\ApplicationInfoEntity;

class ApplicationInfoRepository extends BaseRepository
{

    /**
     * 构造方法
     * @param unknown $config (optional)
     */
    public function __construct($config = [])
    {
        parent::__construct($config);
    }

    public static $APPLICATION_ID = [
        'MemberCenter'=>'ba8d842d-c018-11e5-8e10-005056a65fd7',
        'MobileCheckRoom'=>'ba8a48ff-c018-11e5-8e10-005056a65fd7',
        'Engineer'=>'ba8bd496-c018-11e5-8e10-005056a65fd7',
        'MobileCheckQuality'=>'ca78f8d2-4993-11e6-8304-1051721c3a82',
        'Property'=>'b4b164c5-4a58-11e6-9ad5-005056a65fd7',
        'sms'=>'ba8fd4b6-c018-11e5-8e10-005056a65fd7'
    ];

    /**
     * 根据关键字，获取产品名称列表
     * @param string $keyword 关键字
     * @param int $limit 返回条数，传空：全部返回
     * @return array
     */
    public static function getApplicationInfoByKeyword($keyword, $limit = '')
    {
        $query = ApplicationInfoEntity::find();
        $query->where(['is_show'=>1]);
        $query->andWhere(['or',"BusinessTypeName='SAAS产品'","BusinessTypeName='SAAS其他'","BusinessTypeName='SAAS专项'"]);
        if (!empty($keyword)) {
            $query->where(['like', 'application_name', $keyword]);
        }
        if (intval($limit) > 0) {
            $query->limit($limit);
        }
        return $query->orderBy('created_on ASC')->all();
    }

    public static function getApplicationIdByName($appname){
        $res = ApplicationInfoEntity::find()->select('application_id')->where(['application_name'=>$appname])->scalar();
        if(empty($res)){
            $res = '';
        }
        return $res;
    }

    public static function getApplicationInfoById($appid){
        $res = ApplicationInfoEntity::find()->where(['application_id'=>$appid])->one();
        return $res;
    }

    /**
     * 获取所有产品列表
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getBalanceRateApplicationList()
    {
        $sql = "SELECT
                r.balance_rate_id,
                IF(r.max_number = 999999999,i.application_name,CONCAT(i.application_name,'(',r.min_number,'-',r.max_number,')')) appname,
                r.application_id,
                i.application_name,
                r.min_number,
                r.max_number
                FROM myscrm_application_balance_rate r
                inner join myscrm_application_info i on r.application_id=i.application_id";
        $data = ApplicationInfoEntity::getDb()->createCommand($sql)->queryAll();
        return $data;
    }

    public static function getAppNameByBalanceRateId($BalanceRateId){
        $sql = "SELECT
                IF(r.max_number = 999999999,i.application_name,CONCAT(i.application_name,'(',r.min_number,'-',r.max_number,')')) appname
                FROM myscrm_application_balance_rate r
                inner join myscrm_application_info i on r.application_id=i.application_id
                WHERE r.balance_rate_id='".$BalanceRateId."'";
        $data = ApplicationInfoEntity::getDb()->createCommand($sql)->queryScalar();
        return $data;
    }
}